﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Input;
using Caliburn.Micro;
using ThunderboltHelper;
using ThunderboltInterfaceSharp.Views.UserControls.Download;

namespace ThunderboltInterfaceSharp.ViewModels.UserControls.Download;

public class DownloadCatalogViewModel:Screen
{
	public string DownloadClassification { get; set; }

	public Screen ContentViewModel { get; set; }
	

	//ObservableCollection替换为List方便排序
	public List<SingleDownloadItemViewModel> DownloadItemCollection { get; set; }
	public CompletedItemsViewModel CompletedItems { get; set; }
	public DownloadingItemsViewModel DownloadingItems { get; set; }

	public Screen ViewModelScreen { get; set; }

	public AddDownloadCommandViewModel AddDownloadCommand { get; set; }
	/// <summary> 控制排序功能面板显示</summary>
	public bool IsSortFunctionShow { get; set; }
	public DownloadCatalogViewModel()
	{
		CompletedItems = new CompletedItemsViewModel();
		DownloadingItems = new DownloadingItemsViewModel();
		ViewModelScreen = DownloadingItems;
		DownloadItemCollection = new List<SingleDownloadItemViewModel>();
		DownloadClassification = "下载中";
	
		AddDownloadCommand = new AddDownloadCommandViewModel();
		IsMenuShow = false;
	}
	//todo:ContextMenu样式：
	//圆角
	//用于放图标的左间距
	//字体颜色
	//分割线贯穿
	//弹出位置可以自定义
	public bool IsMenuShow { get; set; }
	public void BtnOtherPreviewLeftDown(object sender, MouseButtonEventArgs e)
	{
		var view = sender as DownloadCatalogView;
		if (view.MenuOther.IsOpen)
		{
			view.MenuOther.IsOpen = false;
		}
		else
		{
			view.MenuOther.IsOpen = true;
		}
		//目标
		view.MenuOther.PlacementTarget = view.BtnOther;
		//位置
		view.MenuOther.Placement = PlacementMode.Left;
		//IsMenuShow = !IsMenuShow;
		////显示菜单
		//view.menu.IsOpen = true;
		//_isMenuShow = true;
	}

	public void BtnOtherPreviewRightDown(object sender, MouseButtonEventArgs e)
	{
			e.Handled = true;
	
	}
	public void BtnOtherRightDown(object sender, MouseButtonEventArgs e)
	{
		e.Handled = true;
	}
	public void BtnContextMenuOpening(object sender, ContextMenuEventArgs  e)
	{
		e.Handled = true;
	}

	public void BtnNewDownload()
	{
		var windowManager = IoC.Get<IWindowManager>();
		windowManager.ShowWindowAsync(AddDownloadCommand);
	}
	public void BtnDownloadContentSortClick()=> IsSortFunctionShow=!IsSortFunctionShow;
	/// <summary> 排序方法</summary>
	public void ItemsSort()
	{
		SortedSet<string> listsSortedSet = new SortedSet<string>(MyStringComparer.Instance);
		DownloadItemCollection.ForEach(s=> listsSortedSet.Add(s.ItemName));
		var temp = new List<SingleDownloadItemViewModel>();
		foreach (string item in listsSortedSet)
		{
			if(string.IsNullOrEmpty(item)) continue;
			
			temp.Add(DownloadItemCollection.FirstOrDefault(s => s.ItemName.Equals(item))??new SingleDownloadItemViewModel());
		}
		DownloadItemCollection = temp;
	}


	public void SwitchChildContent(Classification title)
	{
		ViewModelScreen = title switch
		{
			Classification.Downloading => DownloadingItems,
			Classification.Completed => CompletedItems,
			_ => DownloadingItems
		};
		DownloadClassification = title switch
		{
			Classification.Downloading when DownloadingItems.DownloadItemCollection.Count > 0 => "下载中",
			Classification.Downloading when DownloadingItems.DownloadItemCollection.Count == 0 => "已暂停",
			Classification.Completed => "已完成",
			Classification.CloudAddition=>"云添加",
			Classification.FilmVault => string.Empty,
			Classification.RecycleBin => "回收站",
			Classification.DownloadingPersonal => string.Empty,
			Classification.CompletedPersonal => string.Empty,

			_ => "下载中"
		};
		
	}
}
public enum Classification : byte
{
	Downloading = 0,
	Completed,
	CloudAddition,
	FilmVault,
	RecycleBin,
	DownloadingPersonal,
	CompletedPersonal
}